package com.controller;

import java.util.List;
import java.util.Set;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.pojo.Permission;
import com.pojo.Role;
import com.service.IRoleService;
import com.util.Message;

@Controller
public class RoleController {

	@Resource
	private IRoleService roleService;

	@RequestMapping(value = "/allRolesByPage", method = RequestMethod.POST)
	@ResponseBody
	public Message getroles(@RequestBody Message message) {
		List<Role> list=roleService.getroles(Integer.valueOf(message.getMessage()),message.getGroups());
		//重新获取总数，保证数据事实刷新
		int count = roleService.getCounts();
		Message mess=new Message();
		mess.setRole(list);
		mess.setMessage((int)Math.ceil((float)count/message.getGroups())+"");
		return mess;
	}
	
	@RequestMapping(value = "/allRoles", method = RequestMethod.GET)
	@ResponseBody
	public List<Role> getallRoles() {
		List<Role> list = roleService.getroles();
		return list;
	}

	// 获取总页数
	@RequestMapping(value = "/getRoleCounts", method = RequestMethod.POST)
	@ResponseBody
	public Message getcounts(@RequestBody Message message) {
		// 获取总数
		int count = roleService.getCounts();
		Message mess = new Message();
		mess.setMessage((int) Math.ceil((float) count / message.getGroups()) + "");
		return mess;
	}

	@RequestMapping(value = "/addRole", method = RequestMethod.POST)
	@ResponseBody
	public Role addRole(@RequestBody Role r) {
		Role role = roleService.createRole(r);
		int rid = role.getId();
		Set<Permission> permissions = r.getPermission();
		if (permissions != null && permissions.size() != 0) {
			for (Permission permission : permissions) {
				int pid = permission.getId();
				roleService.correlationPermissions(rid, pid);
			}
		}

		return role;
	}

	@RequestMapping(value = "/deleteRole/{roleid}", method = RequestMethod.DELETE)
	@ResponseBody
	public Message deleteRole(@PathVariable int roleid) {
		roleService.deleteroles(roleid);
		roleService.deleteRole(roleid);
		return new Message("success");
	}

	@RequestMapping(value = "/getRole/{roleid}", method = RequestMethod.GET)
	@ResponseBody
	public Role getRole(@PathVariable int roleid) {
		Role r = roleService.getrolebyid(roleid);
		return r;
	}

	@RequestMapping(value = "/updateRole/{roleid}", method = RequestMethod.PUT)
	@ResponseBody
	public Role updateRole(@PathVariable int roleid, @RequestBody Role r) {
		roleService.updateRole(r);
		Set<Permission> permissions = r.getPermission();
		if (permissions.size() != 0 && permissions != null) {
			roleService.deleteroles(roleid);
			for (Permission permission : permissions) {
				int pid = permission.getId();
				roleService.correlationPermissions(roleid, pid);
			}
		}

		return r;
	}

}
